home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / source.exe / POSIX / ELVIS / MISC.C < prev    next >
C/C++ Source or Header  |  1993-07-06  |  2KB  |  108 lines

  1. /* misc.c */
  2.  
  3. /* Author:
  4.  *    Steve Kirkendall
  5.  *    14407 SW Teal Blvd. #C
  6.  *    Beaverton, OR 97005
  7.  *    kirkenda@cs.pdx.edu
  8.  */
  9.  
  10.  
  11. /* This file contains functions which didn't seem happy anywhere else */
  12.  
  13. #include "config.h"
  14. #include "vi.h"
  15.  
  16.  
  17. /* find a particular line & return a pointer to a copy of its text */
  18. char *fetchline(line)
  19.     long    line;    /* line number of the line to fetch */
  20. {
  21.     int        i;
  22.     REG char    *scan;    /* used to search for the line in a BLK */
  23.     long        l;    /* line number counter */
  24.     static BLK    buf;    /* holds ONLY the selected line (as string) */
  25.     REG char    *cpy;    /* used while copying the line */
  26.     static long    nextline;    /* }  These four variables are used */
  27.     static long    chglevel;    /*  } to implement a shortcut when  */
  28.     static char    *nextscan;    /*  } consecutive lines are fetched */
  29.     static long    nextlnum;    /* }                                */
  30.  
  31.     /* can we do a shortcut? */
  32.     if (changes == chglevel && line == nextline)
  33.     {
  34.         scan = nextscan;
  35.     }
  36.     else
  37.     {
  38.         /* scan lnum[] to determine which block its in */
  39.         for (i = 1; line > lnum[i]; i++)
  40.         {
  41.         }
  42.         nextlnum = lnum[i];
  43.  
  44.         /* fetch text of the block containing that line */
  45.         scan = blkget(i)->c;
  46.  
  47.         /* find the line in the block */
  48.         for (l = lnum[i - 1]; ++l < line; )
  49.         {
  50.             while (*scan++ != '\n')
  51.             {
  52.             }
  53.         }
  54.     }
  55.  
  56.     /* copy it into a block by itself, with no newline */
  57.     for (cpy = buf.c; *scan != '\n'; )
  58.     {
  59.            *cpy++ = *scan++;
  60.     }
  61. #if WIN_NT     /* Added by CSP - To remove ^M */
  62.         if ( *( cpy - 1 ) == '\r' ) 
  63.              cpy--;
  64. #endif
  65.     *cpy = '\0';
  66.  
  67.     /* maybe speed up the next call to fetchline() ? */
  68.     if (line < nextlnum)
  69.     {
  70.         nextline = line + 1;
  71.         chglevel = changes;
  72.         nextscan = scan + 1;
  73.     }
  74.     else
  75.     {
  76.         nextline = 0;
  77.     }
  78.  
  79.     /* Calls to fetchline() interfere with calls to pfetch().  Make sure
  80.      * that pfetch() resets itself on its next invocation.
  81.      */
  82.     pchgs = 0L;
  83.  
  84.     /* Return a pointer to the line's text */
  85.     return buf.c;
  86. }
  87.  
  88.  
  89. /* error message from the regexp code */
  90. void regerror(txt)
  91.     char    *txt;    /* an error message */
  92. {
  93.     msg("RE error: %s", txt);
  94. }
  95.  
  96. /* This function is equivelent to the pfetch() macro */
  97. void    pfetch(l)
  98.     long    l;    /* line number of line to fetch */
  99. {
  100.     if(l != pline || changes != pchgs)
  101.     {
  102.         pline = (l);
  103.         ptext = fetchline(pline);
  104.         plen = strlen(ptext);
  105.         pchgs = changes;
  106.     }
  107. }
  108.